import axios from "./index"
import store from "@/store/index.js";
// token重新认证
export function refreshToken() {
    console.log('刷新token值1');
    return new Promise((resolve, reject) => {
        console.log('store.commit')
        axios({
            method: "post",
            url: '/oauth/token',
            params: {
                grant_type: 'refresh_token',
                client_id: 'pictureShare',
                client_secret: 'pictureShare',
                refresh_token: localStorage.refresh_token
            },
        }).then(res => {
            let {
                code,
                msg,
                result
            } = res;
            console.log(result, '刷新token值2');
            if (code == 200) {
                // 保存用户的信息 刷新成功
                store.commit('m_user/SET_USER_ID', result.user_id);
                store.commit('m_user/SET_accessToken', result.access_token);
                store.commit('m_user/SET_refreshToken', result.refresh_token);
                store.commit('m_user/SET_tokenType', result.token_type);
                resolve(code)
            } else {
                // 删除用户信息
                store.commit('m_user/SET_USER_ID', '-1');
                store.commit('m_user/SET_accessToken', '-1');
                store.commit('m_user/SET_refreshToken', '-1');
                store.commit('m_user/SET_tokenType', '-1');
                store.commit('m_user/UPDATE_USER_INFO', {});
                reject(code)
            }
            reject(code)
        }).catch(err => {
            console.log('报错了', err)
            //     // 删除用户信息
            store.commit('m_user/SET_USER_ID', '-1');
            store.commit('m_user/SET_accessToken', '-1');
            store.commit('m_user/SET_refreshToken', '-1');
            store.commit('m_user/SET_tokenType', '-1');
            store.commit('m_user/UPDATE_USER_INFO', {});
            reject(code)
            reject(err)
        })

    })
}

// 删除本地信息
export function clearAllUserInfo() {
    store.commit('m_user/SET_USER_ID', "-1");
    store.commit('m_user/SET_accessToken', "-1");
    store.commit('m_user/SET_refreshToken', "-1");
    store.commit('m_user/SET_tokenType', "-1");
    store.commit('m_user/UPDATE_USER_INFO', {});
}